#include <hd_led.h>
#include <hd_oled.h>
#include <hd_oled_protocol_i2c.h>
#include <delay.h>
#include <pd_dma.h>
#include <pd_i2c_hd.h>
#include <hd_flash.h>
#include "main.h"


uint8_t dataA[] = {0x01, 0x02, 0x03, 0x04};
uint8_t dataB[4] = {4, 3, 2, 1};


#define OLED_PROTOCOL       2   // Select a protocol. SPI, 1; I2C, 2;
#define OLED_PROTOCOL_IMPL  2   // Select a implement. simulate, 1; hardware, 2;

uint8_t times = 0;
static OLED *oled;

void oled_instance();

extern void SystemClock_Config(void);


int main(void) {

    SystemClock_Config();

    NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_2);

    oled_instance();
    oled->showString(oled, 0, 0, " DMA M2M");

    // 转运前源数据
    oled->printf(oled, 00, 16, "%02X", dataA[0]);
    oled->printf(oled, 24, 16, "%02X", dataA[1]);
    oled->printf(oled, 48, 16, "%02X", dataA[2]);
    oled->printf(oled, 72, 16, "%02X", dataA[3]);

    // 转运前目标数据
    oled->printf(oled, 00, 32, "%02X", dataB[0]);
    oled->printf(oled, 24, 32, "%02X", dataB[1]);
    oled->printf(oled, 48, 32, "%02X", dataB[2]);
    oled->printf(oled, 72, 32, "%02X", dataB[3]);
    oled->refresh(oled);

    DMA *dma = DMABuilder.build(DMA2, (uint32_t) dataA, (uint32_t) dataB, 4, LL_DMA_CHANNEL_1, LL_DMA_STREAM_2);
    dma->init(dma);

    delay_ms(1000);

    // 转运后目标数据
    oled->printf(oled, 00, 48, "%02X", dataB[0]);
    oled->printf(oled, 24, 48, "%02X", dataB[1]);
    oled->printf(oled, 48, 48, "%02X", dataB[2]);
    oled->printf(oled, 72, 48, "%02X", dataB[3]);
    oled->refresh(oled);

    while (1) {}
}

void oled_instance() {
#if OLED_PROTOCOL == 1
    GPIO *sckGPIO = GPIOBuilder.build(GPIOA, LL_GPIO_PIN_5); // Clock, SPIx_SCK
    GPIO *sdaGPIO = GPIOBuilder.build(GPIOA, LL_GPIO_PIN_7); // MOSI,  SPIx_MOSI
    GPIO *resGPIO = GPIOBuilder.build(GPIOA, LL_GPIO_PIN_3); // Reset, General IO, the chip reset pin is better
    GPIO *dcGPIO = GPIOBuilder.build(GPIOA, LL_GPIO_PIN_1);  // Data or Command, General IO
    GPIO *csGPIO = GPIOBuilder.build(GPIOA, LL_GPIO_PIN_2);  // Chip Select(Slave Select), General IO
    SPIGPIO *spiGPIO = SPIGPIOBuilder.build(&csGPIO, 1, sckGPIO, sdaGPIO, NULL);

#if OLED_PROTOCOL_IMPL == 1
    SPISimulate *spiObj = SPISimulateBuilder.build(spiGPIO);
#elif OLED_PROTOCOL_IMPL == 2
    SPIHardware *spiObj = SPIHardwareBuilder.build(SPI1, spiGPIO);
#endif
    SPI *spi1 = (SPI *) spiObj;
    spi1->init(spi1);
    OLEDProtocolSPI *oledProtocolObj = OLEDProtocolSPIBuilder.build(spi1, resGPIO, dcGPIO, csGPIO);

#elif OLED_PROTOCOL == 2
    GPIO *sckGPIO = GPIOBuilder.build(GPIOB, LL_GPIO_PIN_10);  // I2Cx_SCL
    GPIO *sdaGPIO = GPIOBuilder.build(GPIOB, LL_GPIO_PIN_11);  // I2Cx_SDA
#if OLED_PROTOCOL_IMPL == 1
    I2CSimulate *i2CObj = I2CSimulateBuilder.build(sckGPIO, sdaGPIO);
#elif OLED_PROTOCOL_IMPL == 2
    I2CHardware *i2CObj = I2CHardwareBuilder.build(I2C2, sckGPIO, sdaGPIO);
#endif
    I2C *i2c = (I2C *) i2CObj;
    i2c->init(i2c);
    OLEDProtocolI2C *oledProtocolObj = OLEDProtocolI2CBuilder.build(i2c);
#endif

    StorageFlash *storageFlash = StorageFlashBuilder.build();
    OLEDFont *oledFont = OLEDFontBuilder.build((Storage *) storageFlash, ENCODING_UTF8);
    oled = OLEDBuilder.build((OLEDProtocol *) oledProtocolObj, oledFont);
    oled->init(oled);
}

